package com.ryder.petmatediarybackend.module.pet.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ryder.petmatediarybackend.common.response.Result;
import com.ryder.petmatediarybackend.module.pet.dto.CreatePetDTO;
import com.ryder.petmatediarybackend.module.pet.entity.PetProfile;
import com.ryder.petmatediarybackend.module.pet.service.PetProfileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述：宠物档案管理 Controller
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-10-30 20:00:00
 */
@Slf4j
@Tag(name = "宠物档案管理", description = "宠物档案相关接口（需要登录）")
@RestController
@RequestMapping("/pet")
@SecurityRequirement(name = "Authorization")
@RequiredArgsConstructor
public class PetProfileController {

    private final PetProfileService petProfileService;

    @Operation(summary = "创建宠物档案", description = "用户创建新的宠物档案")
    @PostMapping("/create")
    public Result<PetProfile> createPet(@Valid @RequestBody CreatePetDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物档案] 用户ID={} 执行创建宠物档案, 方法=createPet, 入参={}", userId, dto);
        PetProfile petProfile = petProfileService.createPet(userId, dto);
        log.info("[宠物档案] 用户ID={} 创建宠物档案成功, 宠物ID={}, 宠物名称={}", userId, petProfile.getId(), petProfile.getPetName());
        return Result.ok(petProfile);
    }

    @Operation(summary = "获取宠物列表", description = "获取当前用户的所有宠物档案")
    @GetMapping("/list")
    public Result<List<PetProfile>> getPetList() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物档案] 用户ID={} 执行获取宠物列表, 方法=getPetList", userId);
        List<PetProfile> petList = petProfileService.getPetListByUserId(userId);
        log.info("[宠物档案] 用户ID={} 获取宠物列表成功, 数量={}", userId, petList.size());
        return Result.ok(petList);
    }

    @Operation(summary = "获取宠物详情", description = "根据宠物ID获取详细信息")
    @GetMapping("/{petId}")
    public Result<PetProfile> getPetDetail(@PathVariable Long petId) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物档案] 用户ID={} 执行获取宠物详情, 方法=getPetDetail, 宠物ID={}", userId, petId);
        PetProfile petProfile = petProfileService.getPetDetail(userId, petId);
        return Result.ok(petProfile);
    }

    @Operation(summary = "更新宠物档案", description = "更新宠物档案信息")
    @PutMapping("/update/{petId}")
    public Result<Void> updatePet(@PathVariable Long petId, @Valid @RequestBody CreatePetDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物档案] 用户ID={} 执行更新宠物档案, 方法=updatePet, 宠物ID={}, 入参={}", userId, petId, dto);
        petProfileService.updatePet(userId, petId, dto);
        log.info("[宠物档案] 用户ID={} 更新宠物档案成功, 宠物ID={}", userId, petId);
        return Result.ok();
    }

    @Operation(summary = "删除宠物档案", description = "逻辑删除宠物档案")
    @DeleteMapping("/delete/{petId}")
    public Result<Void> deletePet(@PathVariable Long petId) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物档案] 用户ID={} 执行删除宠物档案, 方法=deletePet, 宠物ID={}", userId, petId);
        petProfileService.deletePet(userId, petId);
        log.info("[宠物档案] 用户ID={} 删除宠物档案成功, 宠物ID={}", userId, petId);
        return Result.ok();
    }
}
